package com.registration.configuration;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 消息队列的配置，定义和绑定
 * */
@Configuration
public class RabbitmqConfiguration {

    /**
     * 交换机
     * 预约挂号
     * */
    @Bean
    public TopicExchange exchangetopic(){
        return new TopicExchange("appointment-register");
    }

    /**
     * 死信交换机
     * 预约挂号
     * */
    @Bean
    public TopicExchange exchangetopicdlx(){
        return new TopicExchange("appointmentdlx-register");
    }

    /**
     * 定义队列
     * */
    @Bean
    public Queue queuesms(){
        //return new Queue("registration");
        return QueueBuilder.durable("registration")
                //声明该队列的死信消息发送到的 交换机 （队列添加了这个参数之后会自动与该交换机绑定，并设置路由键，不需要开发者手动设置)
                .withArgument("x-dead-letter-exchange", "appointmentdlx-register")
                //声明该队列死信消息在交换机的 路由键
                .withArgument("x-dead-letter-routing-key", "user-dead-letter-routing-key")
                //设置过期死信时间
                .withArgument("x-message-ttl",10000)
                //队列最大消息数量
                .withArgument("x-max-length", 2)
                .build();
    }

    /**
     * 定义死信队列
     * */
    @Bean
    public Queue queuesmsDlx(){
        //return new Queue("registrationdlx");
        return QueueBuilder.durable("registration-dlx").build();
    }

    /**
     * 绑定队列到交换机
     * */
    @Bean
    public Binding bindingSms(TopicExchange exchangetopic,Queue queuesms){
        return BindingBuilder.bind(queuesms).to(exchangetopic).with("#.sms.#");
    }

    /**
     * 绑定死信队列到交换机
     * */
    @Bean
    public Binding bindingSmsDlx(TopicExchange exchangetopicdlx,Queue queuesmsDlx){
        return BindingBuilder.bind(queuesmsDlx).to(exchangetopicdlx).with("user-dead-letter-routing-key");
    }
}
